package com.ubxtech.web.handler;

import com.ubxtech.web.properties.AuthProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Victor.Xiao
 * @since 2022-01-17 10:54
 **/
@Configuration
@ConditionalOnProperty(prefix = "ubxtech.login.webmvc", name = "enabled", havingValue = "true", matchIfMissing = true)
public class LoginMvcConfig  implements WebMvcConfigurer {

    @Resource
    private LoginHandlerInterceptor loginHandlerInterceptor;
    @Resource
    private AuthProperties authProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        List<String> list = new ArrayList<>(Arrays.asList(
                "/**/*.css",
                "/**/*.js",
                "/**/*.html",
                "/**/*.ico",
                "/**/*.jpg",
                "/**/*.jpeg",
                "/**/*.png",
                "/**/*.gif",
                "/image/**",
                "/fonts/**",
                "/libs/**",
                "/plugins/**",
                "/swagger/**",
                "/**/api-docs/**",
                "/**/api-docs-ext/**",
                "/swagger-resources/**",
                "/webjars/**",
                "/actuator/**",
                "/**/static/**",
                "/**/public/**",
                "/**/sys/login",
                "/error",
                "/websocket/**"
        ));
        if (authProperties.getExcludePath() != null && authProperties.getExcludePath().size() > 0) {
            list.addAll(authProperties.getExcludePath());
        }
        registry.addInterceptor(loginHandlerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(list)
                .order(-20);
    }
}
